home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 351-375 / disk_351 / pdc / libsrc.lzh / LibSrc / StdLib / environment.c < prev    next >
C/C++ Source or Header  |  1990-04-07  |  4KB  |  176 lines

  1. /*
  2.  * Libraries and headers for PDC release 3.3 (C) 1989 Lionel Hummel.
  3.  * PDC Software Distribution (C) 1989 Lionel Hummel and Paul Petersen.
  4.  * PDC I/O Library (C) 1987 by J.A. Lydiatt.
  5.  *
  6.  * This code is freely redistributable upon the conditions that this 
  7.  * notice remains intact and that modified versions of this file not
  8.  * be included as part of the PDC Software Distribution without the
  9.  * express consent of the copyright holders.  No warrantee of any
  10.  * kind is provided with this code.  For further information, contact:
  11.  *
  12.  *  PDC Software Distribution    Internet:                     BIX:
  13.  *  P.O. Box 4006             or hummel@cs.uiuc.edu            lhummel
  14.  *  Urbana, IL  61801-8801       petersen@uicsrd.csrd.uiuc.edu
  15.  */
  16.  
  17. /* Environment.c - Functions to aid in dealing with environment variables
  18.  *
  19.  *    getenv(v)    Returns the string value of environment variable v
  20.  *    setenv(v, s)    Sets environment variable v to string s
  21.  *    putenv(s)    s is in form "variable=value", calls setenv()
  22.  *    ParseEnv(s)    Places the next parsed entry in String or NULL if 
  23.  *                      no more.  String is set to s if s is non-NULL.
  24.  */
  25.  
  26. /* Misc. notes on environment variables:
  27.  * To embed a space in a file name, precede it with the escape delimiter
  28.  * (usually a backslash).
  29.  */
  30.  
  31. #include <stdio.h>
  32. #include <fcntl.h>
  33. #include <errno.h>
  34. #include <ctype.h>
  35.  
  36. #define MAX_ENV_LENGTH (1024)
  37.  
  38. #define ARG_DELIM    ';'
  39. #define ESC_DELIM    '\\'
  40. #define QUOTE_CHAR    '"'
  41.  
  42. char *String = NULL;
  43. char *currChar = NULL;
  44. char  lastChar = '\0';
  45.  
  46. extern void *malloc();
  47.  
  48. char *getenv(v)
  49. char *v;
  50. {
  51.     int  env_file;
  52.     int  size = 0;
  53.     char *tmp_buf, *value;       /* Used for environment filename and input buffer */
  54.     char *return_buf;
  55.     char *ptr;
  56.  
  57.     value = tmp_buf = (char *) malloc(MAX_ENV_LENGTH);
  58.     if (!tmp_buf)
  59.         exit(ENOMEM);
  60.  
  61.     
  62.     if (strncmp(v, "ENV:", 4) == 0)
  63.         strcpy(value, v);
  64.     else
  65.         sprintf(value, "ENV:%s", v);
  66.  
  67.     if ((env_file = open(value, O_RDONLY)) != -1)    {
  68.         size = read(env_file, value, MAX_ENV_LENGTH);
  69.         close(env_file);
  70.     }
  71.     else {
  72.         return(NULL);
  73.     }
  74.  
  75.     value[size] = '\0';
  76.  
  77.     ptr = return_buf = (char *) malloc(size+1);
  78.     if (return_buf)
  79.         while (*ptr++ = *value++);
  80.     else
  81.         exit(ENOMEM);
  82.  
  83.     free(tmp_buf);
  84.  
  85.     return(return_buf);
  86. }
  87.  
  88.  
  89. int setenv(v, s)
  90. char *v;
  91. char *s;
  92. {
  93.     int  env_file;
  94.     char *tmp_buf;
  95.  
  96.     tmp_buf = (char *) malloc(MAX_ENV_LENGTH);
  97.     if (!tmp_buf)
  98.         return(0);
  99.     
  100.     if (strncmp(v, "ENV:", 4) == 0)
  101.         strcpy(tmp_buf, v);
  102.     else
  103.         sprintf(tmp_buf, "ENV:%s", v);
  104.  
  105.     if ((env_file = open(tmp_buf, O_CREAT+O_WRONLY)) != -1)    {
  106.         write(env_file, s, strlen(s));
  107.         close(env_file);
  108.         }
  109.  
  110.     free(tmp_buf);
  111. }
  112.  
  113.  
  114. int putenv(s)
  115. char *s;
  116. {
  117.     char *variable;
  118.     char *value;
  119.  
  120.     variable = strdup(s);
  121.     if (variable)  {
  122.         value = strchr(s, '=');
  123.         if (value != NULL)  {
  124.             *value++ = '\0';
  125.             setenv(variable, value);
  126.             }
  127.         free(variable);
  128.         }
  129. }
  130.  
  131.  
  132. char *ParseEnv(s)
  133. char *s;
  134. {
  135.     char *ptr;
  136.     int   in_quote = 0;
  137.  
  138.     if (s != NULL)  {
  139.         if (String != NULL)
  140.             free(String);
  141.         currChar = String = strdup(s);
  142.         }
  143.  
  144.     s = ptr = currChar;
  145.  
  146.     if (*currChar == '\0')
  147.         return(NULL);
  148.  
  149.     while ( (*currChar != '\0') && (*currChar != ARG_DELIM))    {
  150.         if (*currChar == ESC_DELIM)
  151.              switch (*(currChar+1))    {
  152.                  case QUOTE_CHAR:
  153.                  case ESC_DELIM:
  154.                  case ARG_DELIM:  *ptr++ = *++currChar;
  155.                                    currChar++;
  156.                                    break;
  157.                  default:  *ptr++ = *currChar++;
  158.                             break;
  159.                  }
  160.         else if (isspace(*currChar) && !in_quote)
  161.             currChar++;
  162.         else if (*currChar == QUOTE_CHAR) {
  163.             in_quote = !in_quote;
  164.             currChar++;
  165.             }
  166.         else
  167.             *ptr++ = *currChar++;
  168.         }
  169.  
  170.     if (*currChar == ARG_DELIM)
  171.         currChar++;
  172.  
  173.     *ptr = '\0';
  174.  
  175.     return(s);
  176. }